package com.neueedu.android;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class WebViewLocalActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view_local);
        WebView webview = findViewById(R.id.webview);

        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许js执行




        webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Toast.makeText(WebViewLocalActivity.this, message, Toast.LENGTH_SHORT).show();
                return true;
            }
        });


        //给js 注册一个JAVA对象
        webview.addJavascriptInterface(new WebAppInterface(this), "Android");

        webview.loadUrl("file:android_asset/index.html");


    }




    public class WebAppInterface {
        Context mContext;

        /** Instantiate the interface and set the context. */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /** Show a toast from the web page. */
        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();



        }
    }



}
